package com.zsy.rocketdemo;

import com.zsy.rocketdemo.produce.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.Scanner;

@SpringBootApplication
public class RocketDemoApplication {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(RocketDemoApplication.class, args);
        ZsyAsyncProdecer zsyAsyncProdecer = ApplicationContextHandle.getBean(ZsyAsyncProdecer.class);
        ZsyDefaultProducer zsyDefaultProducer = ApplicationContextHandle.getBean(ZsyDefaultProducer.class);
        ZsyOneWayProducer zsyOneWayProducer = ApplicationContextHandle.getBean(ZsyOneWayProducer.class);
        ZsyDelayProducer zsyDelayProducer = ApplicationContextHandle.getBean(ZsyDelayProducer.class);
        ZsyTransProducer zsyTransProducer = ApplicationContextHandle.getBean(ZsyTransProducer.class);
        while (true) {
            try {
                String s = scanner.nextLine();
                if (StringUtils.equalsIgnoreCase(s, "q")) {
                    SpringApplication.exit(applicationContext, () -> 0);
                    System.exit(0);
                }
                String[] myArgs = s.split(",");
                int num = Integer.parseInt(myArgs[1]);
                if (s.startsWith("zsyDefault")) {
                    zsyDefaultProducer.sendMessage(num);
                } else if (s.startsWith("zsyAsyn")) {
                    zsyAsyncProdecer.sendMessage(num);
                } else if (s.startsWith("zsyOneWay")) {
                    zsyOneWayProducer.sendMessage(num);
                } else if (s.startsWith("zsyDelay")) {
                    zsyDelayProducer.sendMessage(num);
                } else if (s.startsWith("zsyTrans")) {
                    zsyTransProducer.sendMessage(num);
                } else {
                    throw new Exception();
                }
            } catch (Exception e) {
                System.out.println("输入错误");
            }
        }
    }

}
